package jp.co.technotree.common.dao;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

public class StatementParameters {
	public final static int TYPE_INT = 1;
	public final static int TYPE_LONG = 3;
	public final static int TYPE_STRING = 2;
	public final static int TYPE_BIGDECIMAL = 4;
	public final static int TYPE_DATE = 5;
	public final static int TYPE_STRINGFORMAT = 6;
	
	public final static int TYPE_TIME = 7;
	public final static int TYPE_TIMESTAMP = 8;

	private LinkedList<StatementParameter> list = new LinkedList<StatementParameter>();
	private HashMap<String, String> link = new HashMap<String, String>();
	
	
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setInt(String name, int value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_INT;
		param.setValue(new Integer(value));
		list.addLast(param);
	}
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setLong(String name, long value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_LONG;
		param.setValue(new Long(value));
		list.addLast(param);
	}
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setString(String name, String value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_STRING;
		param.setValue(value);
		list.addLast(param);
	}
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setStringLike(String name, String value){
		if ( value != null && !"".equals(value) ){
			StatementParameter param = new StatementParameter();
			param.setName(name);
			param.type = TYPE_STRING;
			param.setValue("%"+value+"%");
			list.addLast(param);
		}
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setStringLikeFirst(String name, String value){
		if ( value != null && !"".equals(value) ){
			StatementParameter param = new StatementParameter();
			param.setName(name);
			param.type = TYPE_STRING;
			param.setValue(""+value+"%");
			list.addLast(param);
		}
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setStringLikeEnd(String name, String value){
		if ( value != null && !"".equals(value) ){
			StatementParameter param = new StatementParameter();
			param.setName(name);
			param.type = TYPE_STRING;
			param.setValue("%"+value+"");
			list.addLast(param);
		}
	}
	
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setStringFormat(String name, String value, String format){
		StatementParameter param = new StatementParameter();
		String[] values = new String[2];
		values[0] = format;
		values[1] = value;
		param.setName(name);
		param.type = TYPE_STRINGFORMAT;
		param.setValue(values);
		list.addLast(param);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setBigDecimal(String name, BigDecimal value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_BIGDECIMAL;
		if ( value == null ){
			param.setValue(BigDecimal.ZERO);
		}else{
			param.setValue(value);
		}
		list.addLast(param);
	}
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setDate(String name, Date value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_DATE;
		
		java.sql.Date sqlDate = null;
		if ( value != null ){
			if ( value instanceof java.util.Date ){
				try{
					sqlDate = new java.sql.Date(value.getTime());
				}catch(Exception ex){
					sqlDate = null;
					ex.printStackTrace();
				}
			}else{
				sqlDate = (java.sql.Date)value;
			}
		}
		param.setValue(sqlDate);
		list.addLast(param);
	}
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setDate(String name, String value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_DATE;
		param.setValue(value);
		list.addLast(param);
	}	

	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setTime(String name, Time value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_TIME;
		param.setValue(value);
		list.addLast(param);
	}
	
	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setTimestamp(String name, Timestamp value){
		StatementParameter param = new StatementParameter();
		param.setName(name);
		param.type = TYPE_TIMESTAMP;
		param.setValue(value);
		list.addLast(param);
	}
	
	protected LinkedList<StatementParameter> getParameters(){
		return list;
	}
	
	protected void setLink(int id,String name){
		link.put(Integer.toString(id),name);
	}
	
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	protected StatementParameter getParameter(String name){
		int i;
		for ( i=0; i<list.size(); i++ ){
			StatementParameter parameter = (StatementParameter)list.get(i);
			if ( name.equals(parameter.name) ){
				return parameter;
			}
		}
		
		return null;
	}

	/**
	 * 
	 * @param idx
	 * @return
	 */
	protected StatementParameter getParameter(int idx){
		StatementParameter parameter = (StatementParameter)list.get(idx);
		return parameter;
	}


	
	/**
	 * 項目長取得
	 * @return
	 */
	public int length(){
		return this.list.size();
	}
	
	
	protected static class StatementParameter{
		private int type = -1;
		private String name = null;
		private Object value = null;
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public Object getValue() {
			return value;
		}
		public void setValue(Object value) {
			this.value = value;
		}
		
	}
	
	public String toString(){
		int i;
		String string = "";
		for ( i=0; i<list.size(); i++ ){
			StatementParameter parameter = (StatementParameter)list.get(i);
			string += parameter.getName() + ":";
			switch(parameter.getType()){
			case TYPE_INT:
				Integer integerValue = (Integer)parameter.getValue();
				string += "INT:"+integerValue.intValue();
				break;
			case TYPE_LONG:
				Long longValue = (Long)parameter.getValue();
				string += "LONG:"+longValue.longValue();
				break;
			case TYPE_STRING:
				String stringValue = (String)parameter.getValue();
				string += "STRING:"+stringValue;
				break;
			case TYPE_STRINGFORMAT:
				String[] stringsValue = (String[])parameter.getValue();
				string += "STRING:"+stringsValue[1]+":"+stringsValue[0];
				break;
			case TYPE_BIGDECIMAL:
				BigDecimal decimalValue = (BigDecimal)parameter.getValue();
				string += "BIGDECIMAL:"+decimalValue.toString();
				break;
			case TYPE_DATE:
				SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy/MM/dd");
				Date date = (Date)parameter.getValue();
				if ( date != null ){
					String stringDateValue = sdfDate.format(date);
					string += "DATE:"+stringDateValue;
				}else{
					string += "DATE:"+date;
				}
				break;
			}
			string += "\n";
		}
		return string;
	}
	
}
